Public Class frmBaoCaoTaiChinhTheoKyQT
    Private m_baocao As String

    Public Sub New(ByVal loai_baocao As String)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_baocao = loai_baocao

        If (m_baocao = "TongHopHangMuaNhapKho") Then
            lblBaoCao.Text = "TỔNG HỢP HÀNG MUA NHẬP KHO"
            Me.Text = "TỔNG HỢP HÀNG MUA NHẬP KHO"
            KhoiTaoDMTCNX("N")
        ElseIf (m_baocao = "TongHopHangXuatChoLL-TT") Then
            lblBaoCao.Text = "TỔNG HỢP HÀNG XUẤT CHO LL-TT"
            Me.Text = "TỔNG HỢP HÀNG XUẤT CHO LL-TT"
            KhoiTaoDMTCNX("X")
        ElseIf (m_baocao = "BaoCaoNhapXuatTheoTCNX") Then
            lblBaoCao.Text = "TỔNG HỢP HÀNG NHẬP XUẤT THEO TCNX"
            Me.Text = "TỔNG HỢP HÀNG NHẬP XUẤT THEO TCNX"
            'KHOI TAO CBBNHAPXUAT 
            cbbNhapXuat.Visible = True
            lblNhapXuat.Visible = True
            rbtKyQT.Location = New Point(rbtKyQT.Location.X, rbtKyQT.Location.Y + 10)
            rbtNgay.Location = New Point(rbtNgay.Location.X, rbtNgay.Location.Y + 10)
            'lblNhapXuat.Location = New Point(lblNhapXuat.Location.X, lblNhapXuat.Location.Y - 10)
            'cbbNhapXuat.Location = New Point(cbbNhapXuat.Location.X, cbbNhapXuat.Location.Y - 10)
            'btnBaoCao.Location = New Point(btnBaoCao.Location.X, btnBaoCao.Location.Y + 10)
            'lblKyQT.Location = New Point(lblKyQT.Location.X, lblKyQT.Location.Y + 30)
            'cbbKYQT.Location = New Point(cbbKYQT.Location.X, cbbKYQT.Location.Y + 30)
            'GroupBox1.Location = New Point(GroupBox1.Location.X, GroupBox1.Location.Y + 20)
            'Label1.Location = New Point(Label1.Location.X, Label1.Location.Y + 25)
            'txtTieuDe.Location = New Point(txtTieuDe.Location.X, txtTieuDe.Location.Y + 25)
            'khoi tao cbbnhapxuat
            cbbNhapXuat.Items.Add("Nhập")
            cbbNhapXuat.Items.Add("Xuất")
            cbbNhapXuat.SelectedIndex = 0
            cbbNhapXuat.Focus()
        End If
    End Sub

    Private Sub frmBaoCaoTaiChinhTheoKyQT_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rbtSLKH.Checked = True
        rbtKyQT.Checked = True
        KhoiTaocbbKyQuyetToan()
        cbbKYQT.SelectedIndex = 0
    End Sub

#Region "KhoiTao"
    Public Sub KhoiTaoDMTCNX(ByVal loai As String)
        Dim ary As ArrayList
        If (loai = "N") Then
            ary = CDoiTuongs.GetDoiTuongList(enumDOITUONG.TINHCHATNHAP)
        Else
            ary = CDoiTuongs.GetDoiTuongList(enumDOITUONG.TINHCHATXUAT)
        End If
        clbxTCNX.Items.Clear()
        For Each item As CDoiTuong In ary
            clbxTCNX.Items.Add(item)
        Next
    End Sub

    Public Sub KhoiTaocbbKyQuyetToan()
        Dim ds As DataSet = BaseDB.ExecSql_DataSet("select * from tblKyQT")
        cbbKYQT.DisplayMember = "tenkyqt"
        cbbKYQT.ValueMember = "id"
        Dim row As DataRow
        row = ds.Tables(0).NewRow
        row("id") = 0
        row("tenkyqt") = "Cả năm"
        ds.Tables(0).Rows.Add(row)
        cbbKYQT.DataSource = ds.Tables(0)
    End Sub
#End Region

    Private Sub btnBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaoCao.Click
        If (clbxTCNX.CheckedItems.Count = 0) Then
            ShowMessages("Bạn chưa chọn TCNX cần báo cáo")
            Exit Sub
        Else
            If (txtTieuDe.Text = "") Then
                ShowMessages("Bạn chưa nhập tên báo cáo.")
                Exit Sub
                txtTieuDe.Focus()
            End If
        End If
        '
        Dim KyQT As String
        KyQT = cbbKYQT.SelectedValue
        Dim KyQT_BaoCao As String
        If (cbbKYQT.SelectedValue = 0) Then 'ca nam
            KyQT_BaoCao = "Năm " + CStr(Now.Year)
        Else
            KyQT_BaoCao = cbbKYQT.Text + " - Năm " + CStr(Now.Year)
        End If
        '
        Dim loai_SLBaoCao As String
        If (rbtSLKH.Checked) Then
            loai_SLBaoCao = "SLKH"
        Else
            loai_SLBaoCao = "SLTH"
        End If
        '
        Dim strsql As String
        Dim ds As DataSet
        Dim nnreport As Object = Nothing
        If (m_baocao = "TongHopHangMuaNhapKho") Then
            strsql = "select * from viewDPNX where NX = 'N' "
            If rbtKyQT.Checked Then
                If (cbbKYQT.SelectedValue <> 0) Then 'KyQT = Ca Nam
                    strsql &= " and KYQT = '" & cbbKYQT.SelectedValue & "'"
                End If
            Else
                strsql &= " AND NGAY >= '" & dtpTuNgay.Value.ToString("MM/dd/yyyy") & "' AND NGAY <= '" & dtpDenNgay.Value.ToString("MM/dd/yyyy") & "'"
            End If
            
            If (clbxTCNX.CheckedItems.Count <> 0) Then
                strsql &= " and( "
            End If
            Dim stt As Integer = 1
            For Each itemChecked As Object In Me.clbxTCNX.CheckedItems
                Dim objItem As CDoiTuong = itemChecked
                If (stt = 1) Then
                    strsql &= " TCNX = '" & objItem.KH & "'"
                Else
                    strsql &= " or TCNX = '" & objItem.KH & "'"
                End If
                stt += 1
            Next
            If (clbxTCNX.CheckedItems.Count <> 0) Then
                strsql &= " )"
            End If
            '
            ds = BaseDB.ExecSql_DataSet(strsql)
            nnreport = New rptBaoCaoHangMuaNhapKho
            nnreport.SetDataSource(ds.Tables(0))
            'set parametter
            Dim tongtien As Double
            Dim tongtien_XN150, tongtien_XN150_TBBHC, tongtien_XN150_TB, tongtien_XN150_DC As Double
            Dim row As DataRow
            For Each row In ds.Tables(0).Rows
                tongtien += row("TGIATRI")
                If (row("DVGN") = "053") Then
                    tongtien_XN150 += row("TGIATRI")
                    If (Not row("LOAIMH") Is DBNull.Value) Then
                        If (row("LOAIMH") = "Y0100000000000") Then
                            tongtien_XN150_TB += row("TGIATRI")
                        ElseIf (row("LOAIMH") = "Y0200000000000") Then
                            tongtien_XN150_DC += row("TGIATRI")
                        ElseIf (row("LOAIMH") = "Y0500000000000") Or (row("LOAIMH") = "Y0600000000000") Or (row("LOAIMH") = "Y0700000000000") Or (row("LOAIMH") = "Y0800000000000") Then
                            tongtien_XN150_TBBHC += row("TGIATRI")
                        End If
                    End If
                End If
            Next
            nnreport.SetParameterValue("tongtien_XN150", tongtien_XN150)
            nnreport.SetParameterValue("tongtien_XN150_THCBB", tongtien_XN150_TBBHC)
            nnreport.SetParameterValue("tongtien_XN150_TB", tongtien_XN150_TB)
            nnreport.SetParameterValue("tongtien_XN150_DC", tongtien_XN150_TB)
            nnreport.SetParameterValue("tien_chu", DoiSoRaChu(tongtien))
            If rbtKyQT.Checked Then
                nnreport.SetParameterValue("thoigian", KyQT_BaoCao)
            Else
                nnreport.SetParameterValue("thoigian", "Từ ngày " + dtpTuNgay.Value.ToString("dd/MM/yyyy") + " đến ngày " + dtpDenNgay.Value.ToString("dd/MM/yyyy"))
            End If
            nnreport.SetParameterValue("tieude", txtTieuDe.Text.ToUpper)
        ElseIf (m_baocao = "TongHopHangXuatChoLL-TT") Then
            strsql = "select * from viewTongHopHangXuatChoLLTT where NX = 'X' "
            If rbtKyQT.Checked Then
                If (cbbKYQT.SelectedValue <> 0) Then 'KyQT = Ca Nam
                    strsql &= " and KYQT = '" & cbbKYQT.SelectedValue & "'"
                End If
            Else
                strsql &= " AND NGAY >= '" & dtpTuNgay.Value.ToString("MM/dd/yyyy") & "' AND NGAY <= '" & dtpDenNgay.Value.ToString("MM/dd/yyyy") & "'"
            End If

            If (clbxTCNX.CheckedItems.Count <> 0) Then
                strsql &= " and( "
            End If
            Dim stt As Integer = 1
            For Each itemChecked As Object In Me.clbxTCNX.CheckedItems
                Dim objItem As CDoiTuong = itemChecked
                If (stt = 1) Then
                    strsql &= " TCNX = '" & objItem.KH & "'"
                Else
                    strsql &= " or TCNX = '" & objItem.KH & "'"
                End If
                stt += 1
            Next
            If (clbxTCNX.CheckedItems.Count <> 0) Then
                strsql &= " )"
            End If
            ds = BaseDB.ExecSql_DataSet(strsql)
            Dim tongtien As Double
            For Each row As DataRow In ds.Tables(0).Rows
                tongtien += row("TGIATRI")
            Next
            nnreport = New rptTongHopHangXuatChoLL_TT
            nnreport.SetDataSource(ds.Tables(0))
            nnreport.SetParameterValue(0, DoiSoRaChu(tongtien))
            If rbtKyQT.Checked Then
                nnreport.SetParameterValue(1, KyQT_BaoCao)
            Else
                nnreport.SetParameterValue(1, "Từ ngày " + dtpTuNgay.Value.ToString("dd/MM/yyyy") + " đến ngày " + dtpDenNgay.Value.ToString("dd/MM/yyyy"))
            End If

            nnreport.SetParameterValue("tieude", txtTieuDe.Text.ToUpper)
        ElseIf (m_baocao = "BaoCaoNhapXuatTheoTCNX") Then
            Dim nx, tieude As String
            If (cbbNhapXuat.SelectedIndex = 0) Then
                nx = "N"
            Else
                nx = "X"
            End If
            If Not rbtKyQT.Checked Then
                KyQT_BaoCao = "Từ ngày " + dtpTuNgay.Value.ToString("dd/MM/yyyy") + " đến ngày " + dtpDenNgay.Value.ToString("dd/MM/yyyy")
            End If

            tieude = txtTieuDe.Text.ToUpper & Chr(10) & KyQT_BaoCao
            '
            strsql = "select * from viewBaoCaoHangMua where NX= '" & nx & "' "

            If rbtKyQT.Checked Then
                If (cbbKYQT.SelectedValue <> 0) Then 'KyQT = Ca Nam
                    strsql &= " and KYQT = '" & cbbKYQT.SelectedValue & "'"
                End If
            Else
                strsql &= " AND NGAY >= '" & dtpTuNgay.Value.ToString("MM/dd/yyyy") & "' AND NGAY <= '" & dtpDenNgay.Value.ToString("MM/dd/yyyy") & "'"
            End If

            If (clbxTCNX.CheckedItems.Count <> 0) Then
                strsql &= " and( "
            End If
            Dim stt As Integer = 1
            For Each itemChecked As Object In Me.clbxTCNX.CheckedItems
                Dim objItem As CDoiTuong = itemChecked
                If (stt = 1) Then
                    strsql &= " TCNX = '" & objItem.KH & "'"
                Else
                    strsql &= " or TCNX = '" & objItem.KH & "'"
                End If
                stt += 1
            Next
            If (clbxTCNX.CheckedItems.Count <> 0) Then
                strsql &= " )"
            End If
            strsql &= " order by DVGN, SOPHIEU"

            ds = BaseDB.ExecSql_DataSet(strsql)
            Dim tongtien As Double
            Dim row As DataRow
            For Each row In ds.Tables(0).Rows
                If (rbtSLKH.Checked) Then
                    tongtien += row.Item("gle") * row.Item("SLKH")
                Else
                    tongtien += row.Item("gle") * row.Item("SLTH")
                End If
            Next
            nnreport = New rptBaoCaoHangMua
            nnreport.SetDataSource(ds.Tables(0))
            nnreport.SetParameterValue(0, loai_SLBaoCao)
            nnreport.SetParameterValue("tien", DoiSoRaChu(tongtien))
            nnreport.SetParameterValue("tieude", tieude)
        End If
        Dim frm As New frmReportViewer(nnreport)
        frm.ShowDialog()
    End Sub

    Private Sub cbbNhapXuat_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbbNhapXuat.SelectedIndexChanged
        If (cbbNhapXuat.SelectedIndex = 0) Then
            KhoiTaoDMTCNX("N")
        Else
            KhoiTaoDMTCNX("X")
        End If
    End Sub

    Private Sub rbtKyQT_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtKyQT.CheckedChanged
        If rbtKyQT.Checked Then
            lblTuNgay.Visible = False
            lblDenNgay.Visible = False
            dtpTuNgay.Visible = False
            dtpDenNgay.Visible = False
            lblKyQT.Visible = True
            cbbKYQT.Visible = True
        Else
            lblTuNgay.Visible = True
            lblDenNgay.Visible = True
            dtpTuNgay.Visible = True
            dtpDenNgay.Visible = True
            lblKyQT.Visible = False
            cbbKYQT.Visible = False
        End If
    End Sub
End Class